import json
import pickle
import os

fosfile = open('fos')
rootdir = '/media/shzhuoyu/新加卷/data/mag/'
#拿到保存的fos树
fos = set()
fosdict = {}
line = fosfile.readline()
while line:
    line = line.replace('\n','')
    fos.add(line)#获取fos集合
    fosdict[line]=0
    line = fosfile.readline()


for  directory, subdirectory,filenames in  os.walk(rootdir):
    for filename in filenames:
        print("we begin begin :" + filename)
        file = open(rootdir+filename)
        jsonline = file.readline()
        while jsonline:
            dict = json.loads(jsonline)
            tempfos = dict.get('fos')
            if tempfos:
                tempfos = eval(str(tempfos))
                # print(tempfos)
                for item in tempfos:
                    if item in fos:
                        fosdict[item]+=1
            jsonline = file.readline()
        print("we just finish :"+filename)
        with open('dict' + '.pkl', 'wb') as f:
            pickle.dump(fosdict, f, pickle.HIGHEST_PROTOCOL)
